<?php
	class Default_Model_Category extends Zendvn_Model_Nested
	{
		protected $_name = 'category';
		protected $_primary = 'id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
        
        public function listCategory()
		{
            $data = array(
                'id',
                'name',
                'parents',
                'url',
            );
			$sql = $this->_db->select()
							 ->from($this->_name)
                             ->where('status = 1')
                             ->where('level <> 0')
							 ->order('lft ASC');
			$result = $this->_db->fetchAll($sql);
			return $result;
		}
        
        public function getItemForPaginator($id)
        {
            $data = array(
                'id',
                'url',
            );
			$sql = $this->_db->select()
							 ->from($this->_name)
                             ->where('status = 1')
                             ->where('id = ?', $id);
			$result = $this->_db->fetchRow($sql);
			return $result;
        }
        
        public function getName($id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name, array('name'))
                            ->where('id = ?', $id)
                            ->where('status = 1');
            $result = $this->_db->fetchOne($sql);
            return $result;
        }
        
        public function getChild($id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name, array('id','name','url'))
                            ->where('parents = ?', $id)
                            ->where('status = 1');
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function listCategoryOrder()
        {
            $category = array(
                'c.id',
                'c.name',
            );
            $sql = $this->_db->select()
                            ->from('category AS c', $category)
                            ->joinLeft('product AS p', 'c.id = p.category_id', null)
                            ->where('p.status = 1')
                            ->group('p.category_id')
                            ->order('MAX(p.id) DESC');
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
    }
